<?php
/**
 * Created by PhpStorm.
 * User: ybjaa
 * Date: 2017/5/8
 * Time: 11:15
 */

namespace App\Business\Menu;


use App\Business\BusBase;
use App\Models\menus_manage;
use Illuminate\Support\Facades\DB;

class DeleteMenu extends BusBase
{

    public $ID;



    protected function Handle()
    {
        return $this->DeleteMenu();
    }

    public function GetBusinessName()
    {
        return "删除菜单";
    }


    protected function DeleteMenu()
    {


        if(!$this->ID) return ["result"=>false,"message"=>"参数错误"];

        //开启事务

        DB::beginTransaction();

        //读出此节点
        $cmenu= menus_manage::find($this->ID);

        if(!$cmenu)
        {
            DB::rollBack();
            return ["result"=>false,"message"=>"参数错误"];
        }

        //1.检测该节点是否是叶子结点
        $count=menus_manage::where('parentID',$cmenu->ID)->count();

        if($count>0)
        {
            DB::rollBack();
            return ["result"=>false,"message"=>"参数错误"];
        }


        //2.查找当前层此节点且尝试取出前节点


        $premenu=null;

        $premenu=menus_manage::where('nextID',$cmenu->ID)->first();

        if($premenu)
        {
            $premenu->nextID=$cmenu->nextID;
            $premenu->save();
        }

        $cmenu->delete();


        DB::commit();

        return ["result"=>true];



    }


}